#include "Knifebot.h"

Knifebot g_pKnifebot;

void Knifebot::Init()
{
	Active = true;
}

void Knifebot::Enable()
{
	Active = true;
}

void Knifebot::Disable()
{
	Active = false;
}

void Knifebot::Set(bool bActive)
{
	Active = bActive;
}

void Knifebot::Compute(CUserCmd* cmd)
{
	if(!Active)
		return;

	Entity* pMe = g_pInterfaces.g_pEntList->GetClientEntity(g_pInterfaces.g_pEngine->GetLocalPlayer());

	if(!pMe || !GameUtilities::HasKnife())
		return;

	SD_Tools::Vector vMin, vMax, vecPosition, vPlayer;
	JUNK_CODE;
	SD_Tools::Angle angles, TempAngles;
	
	SD_Tools::Vector m_Eye = pMe->GetEyePosition();

	Trace_t tr;
	Ray_t ray;
	SD_Tools::Angle fTraceVec;
	SD_Tools::Vector Dir, Origin, Forward, Right, Up;
	player_info_t pInfo;

	fTraceVec = cmd->viewangles;	
	JUNK_CODE;

	SD_Tools::MathHelper::AngleToVectors(fTraceVec, &Forward, &Right, &Up);

	Origin = SD_Tools::MathHelper::MulVector(Forward, 60.0f);
	Origin.Add(m_Eye);

	ray.Init(m_Eye, Origin);

	g_pInterfaces.g_pEngineTrace->TraceRay(ray, MASK_NPCWORLDSTATIC|CONTENTS_SOLID|CONTENTS_MOVEABLE|CONTENTS_MONSTER|CONTENTS_WINDOW|CONTENTS_DEBRIS|CONTENTS_HITBOX, NULL, &tr);

	if(tr.m_pEnt && !(tr.m_pEnt->dormant()) && g_pInterfaces.g_pEngine->GetPlayerInfo(tr.m_pEnt->GetIndex(), &pInfo) && *tr.m_pEnt->iTeamNum() != *pMe->iTeamNum() && *tr.m_pEnt->lifeState() == 0)
	{
		cmd->buttons |= IN_ATTACK2;
	}
}